﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.Permission;
using B.S.BasicData.Api.Read.DTOS;
using B.S.BasicData.Domain.Entity;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infastructure.Interface;
using MediatR;

namespace B.S.BasicData.Api.Read.Application.Handler.Permission
{
    public class GetRolepermissionHandler : IRequestHandler<GetRolepermissionCommand, ApiResult<List<RolePermissionDto>>>
    {
        private readonly IBaseRepository<RolePermissionModel> _PermissionRepository;
        private readonly IMapper _mapper;

        public GetRolepermissionHandler(IBaseRepository<RolePermissionModel> permissionRepository, IMapper mapper)
        {
            _PermissionRepository = permissionRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 获取角色权限
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<List<RolePermissionDto>>> Handle(GetRolepermissionCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<RolePermissionDto>> result = new ApiResult<List<RolePermissionDto>>();

            var rolepermission = _PermissionRepository.GetAll().Where(p => p.RId == request.RId).ToList();
            result.Code = StatusEnum.OK;
            result.Data = _mapper.Map<List<RolePermissionDto>>(rolepermission);
            result.Message = "获取成功";
            return Task.FromResult(result);
        }
    }
}
